from GraphGroup import GraphGroup
from rpy2 import robjects


def relativeError(a, b): return abs((a - b) / b)


class GraphGroupMath(GraphGroup):
    
    def calculateTiRelativeErrors(self, topologicalIndex, initialGraph):
        TiG = topologicalIndex.value(initialGraph)
        relativeErrors = []
        for graph in  self.graphs:                
            TiH = topologicalIndex.value(graph)
            relativeErrors.append(relativeError(TiG, TiH))
        return relativeErrors
    
    def calculateTiDiffs(self, topologicalIndex, initialGraph):
        TiG = topologicalIndex.value(initialGraph)
        diffs = []
        for graph in  self.graphs:                
            TiH = topologicalIndex.value(graph)
            diffs.append(abs(TiG - TiH))
        return diffs

    
    def calculateStandardDeviation(self, topologicalIndex):  
        indexName = topologicalIndex.name()
        vector = robjects.FloatVector(self.Tis[indexName])
        return robjects.r.sd(vector).__getitem__(0)

    
    def calculateQuantile(self, quantile, topologicalIndex):  
        indexName = topologicalIndex.name()
        vector = robjects.FloatVector(self.Tis[indexName])
        return robjects.r.quantile(vector,quantile).__getitem__(0)
        
    
    